/*
 * OpenParts
 * A dynamic-loading components framework for GWT
 * Copyright (C) 2011 Christophe Bouthier  [chris{AT}binary-gastronome{DOT}fr]
 *
 * This work is partially based on work I have done at INRIA (http://www.inria.fr) 
 * in the context of the Qualipso European Project (http://qualipso.org/),
 * The work done at INRIA is Copyright (C) 2006-2011 INRIA
 *
 * This work is distributed under the LGPL version 3
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation. See the GNU
 * Lesser General Public License in LGPL.txt for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package fr.openparts.OpenParts.test.functionnal;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;

import com.thoughtworks.selenium.Selenium;

/**
 * Selenium functional test of Proxy Commands on the OpenParts Showcase
 * 
 * @author Christophe Bouthier [chris{AT}binary-gastronome{DOT}fr]
 * @creation.date 05 May 2011
 */
public class SeleniumTestProxyCommands {

    @Test
    public void testProxyCommands() throws InterruptedException {
        WebDriver driver = new FirefoxDriver();

        String baseUrl = "http://127.0.0.1:8080/";
        Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);

        // load the page and test it's loaded
        selenium.open("/index.html");
        selenium.waitForPageToLoad("30000");
        assertTrue(selenium.isElementPresent("//body[@id='fr-openparts']"));
        assertTrue(selenium.isTextPresent("OpenParts Showcase"));

        // loading the page load the showcase module - test showcase module is loaded
        Thread.sleep(2000);
        assertTrue(selenium.isTextPresent("Proxy Commands"));

        // load the widget container module and test it's loaded
        selenium.click("//body[@id='fr-openparts']/div[2]/div[2]/div/div[2]/div/button[6]");
        for (int slot = 0;; slot++) {
            if (slot >= 25)
                fail("timeout");
            try {
                if (selenium.isTextPresent("Loading...")) {
                    break;
                }
            } catch (Exception e) {}
            Thread.sleep(100);
        }
        assertTrue(selenium.isTextPresent("View Container"));

        // loading the page load the showcase module - test showcase module is loaded
        Thread.sleep(2000);
        assertTrue(selenium.isTextPresent("View Embedded"));

        // check that no style has been put yet
        assertFalse(selenium.isElementPresent("css=div.uniqueStyle"));
        assertFalse(selenium.isElementPresent("css=div.uniqueStyle-selected"));
        assertFalse(selenium.isElementPresent("css=div.additionalStyle"));
        assertFalse(selenium.isElementPresent("css=div.primaryStyle"));
        assertFalse(selenium.isElementPresent("css=div.primaryStyle-selected"));

        // set the style name and test if the style name was put
        selenium.click("//div[@id='OP-widgetcontainer-widgetcontainer-display1']/div/div[3]/div/button[1]");
        assertTrue(selenium.isElementPresent("css=div.uniqueStyle"));

        // check that nothing else was added
        assertFalse(selenium.isElementPresent("css=div.uniqueStyle-selected"));
        assertFalse(selenium.isElementPresent("css=div.additionalStyle"));
        assertFalse(selenium.isElementPresent("css=div.primaryStyle"));
        assertFalse(selenium.isElementPresent("css=div.primaryStyle-selected"));

        // add a style name and test if the style name was added
        selenium.click("//div[@id='OP-widgetcontainer-widgetcontainer-display1']/div/div[3]/div/button[2]");
        assertTrue(selenium.isElementPresent("css=div.additionalStyle"));

        // check that the primary style name is still here
        assertTrue(selenium.isElementPresent("css=div.uniqueStyle"));

        // check that nothing else was added
        assertFalse(selenium.isElementPresent("css=div.uniqueStyle-selected"));
        assertFalse(selenium.isElementPresent("css=div.primaryStyle"));
        assertFalse(selenium.isElementPresent("css=div.primaryStyle-selected"));

        // remove the second style name and test if the style name was removed
        selenium.click("//div[@id='OP-widgetcontainer-widgetcontainer-display1']/div/div[3]/div/button[3]");
        assertFalse(selenium.isElementPresent("css=div.additionalStyle"));

        // check that the primary style name is still here
        assertTrue(selenium.isElementPresent("css=div.uniqueStyle"));

        // check that nothing else was added
        assertFalse(selenium.isElementPresent("css=div.uniqueStyle-selected"));
        assertFalse(selenium.isElementPresent("css=div.primaryStyle"));
        assertFalse(selenium.isElementPresent("css=div.primaryStyle-selected"));

        // add a style dependent name and test if the style dependent name was added
        selenium.click("//div[@id='OP-widgetcontainer-widgetcontainer-display1']/div/div[3]/div/button[4]");
        assertTrue(selenium.isElementPresent("css=div.uniqueStyle-selected"));

        // check that the primary style name is still here
        assertTrue(selenium.isElementPresent("css=div.uniqueStyle"));

        // check that nothing else was added
        assertFalse(selenium.isElementPresent("css=div.additionalStyle"));
        assertFalse(selenium.isElementPresent("css=div.primaryStyle"));
        assertFalse(selenium.isElementPresent("css=div.primaryStyle-selected"));

        // remove the style dependent name and test if the style dependent name was removed
        selenium.click("//div[@id='OP-widgetcontainer-widgetcontainer-display1']/div/div[3]/div/button[5]");
        assertFalse(selenium.isElementPresent("css=div.uniqueStyle-selected"));

        // check that the primary style name is still here
        assertTrue(selenium.isElementPresent("css=div.uniqueStyle"));

        // check that nothing else was added
        assertFalse(selenium.isElementPresent("css=div.additionalStyle"));
        assertFalse(selenium.isElementPresent("css=div.primaryStyle"));
        assertFalse(selenium.isElementPresent("css=div.primaryStyle-selected"));

        // put back additional and dependent names and test that they are here
        selenium.click("//div[@id='OP-widgetcontainer-widgetcontainer-display1']/div/div[3]/div/button[2]");
        selenium.click("//div[@id='OP-widgetcontainer-widgetcontainer-display1']/div/div[3]/div/button[4]");
        assertTrue(selenium.isElementPresent("css=div.additionalStyle"));
        assertTrue(selenium.isElementPresent("css=div.uniqueStyle-selected"));

        // check that the primary style name is still here
        assertTrue(selenium.isElementPresent("css=div.uniqueStyle"));

        // check that nothing else was added
        assertFalse(selenium.isElementPresent("css=div.primaryStyle"));
        assertFalse(selenium.isElementPresent("css=div.primaryStyle-selected"));

        // change the style primary name and test if the style primary name was changed
        selenium.click("//div[@id='OP-widgetcontainer-widgetcontainer-display1']/div/div[3]/div/button[6]");
        assertTrue(selenium.isElementPresent("css=div.primaryStyle"));
        assertFalse(selenium.isElementPresent("css=div.uniqueStyle"));

        // check that style dependent name was updated
        assertTrue(selenium.isElementPresent("css=div.primaryStyle-selected"));
        assertFalse(selenium.isElementPresent("css=div.uniqueStyle-selected"));

        // check that style additional name wasn't updated
        assertTrue(selenium.isElementPresent("css=div.additionalStyle"));

        // set the style name, removing all others, and test if style name is here and all others removed
        selenium.click("//div[@id='OP-widgetcontainer-widgetcontainer-display1']/div/div[3]/div/button[1]");
        assertTrue(selenium.isElementPresent("css=div.uniqueStyle"));
        assertFalse(selenium.isElementPresent("css=div.uniqueStyle-selected"));
        assertFalse(selenium.isElementPresent("css=div.additionalStyle"));
        assertFalse(selenium.isElementPresent("css=div.primaryStyle"));
        assertFalse(selenium.isElementPresent("css=div.primaryStyle-selected"));

        // check that the width and height of the view have not been set yet
        assertFalse(selenium.isElementPresent("//div[@class='uniqueStyle' and contains(@style, 'width: 100px')]"));
        assertFalse(selenium.isElementPresent("//div[@class='uniqueStyle' and contains(@style, 'height: 100px')]"));

        // set the width of the view and test that it is at 100px
        selenium.click("//div[@id='OP-widgetcontainer-widgetcontainer-display1']/div/div[3]/div/button[7]");
        assertTrue(selenium.isElementPresent("//div[@class='uniqueStyle' and contains(@style, 'width: 100px')]"));

        // check that height wasn't changed
        assertFalse(selenium.isElementPresent("//div[@class='uniqueStyle' and contains(@style, 'height: 100px')]"));

        // set the height of the view and test that it is at 100px
        selenium.click("//div[@id='OP-widgetcontainer-widgetcontainer-display1']/div/div[3]/div/button[8]");
        assertTrue(selenium.isElementPresent("//div[@class='uniqueStyle' and contains(@style, 'height: 100px')]"));

        // check that width wasn't changed
        assertTrue(selenium.isElementPresent("//div[@class='uniqueStyle' and contains(@style, 'width: 100px')]"));

        // set the size of the view and test that width and height were changed
        selenium.click("//div[@id='OP-widgetcontainer-widgetcontainer-display1']/div/div[3]/div/button[9]");
        assertTrue(selenium.isElementPresent("//div[@class='uniqueStyle' and contains(@style, 'width: 200px')]"));
        assertTrue(selenium.isElementPresent("//div[@class='uniqueStyle' and contains(@style, 'height: 250px')]"));

        // check that the view is visible
        assertTrue(selenium.isVisible("css=div.uniqueStyle"));

        // set the view as not visible and test that it is not visible
        selenium.click("//div[@id='OP-widgetcontainer-widgetcontainer-display1']/div/div[3]/div/button[10]");
        assertFalse(selenium.isVisible("css=div.uniqueStyle"));

        // set the view as visible again and test that it is visible
        selenium.click("//div[@id='OP-widgetcontainer-widgetcontainer-display1']/div/div[3]/div/button[10]");
        assertTrue(selenium.isVisible("css=div.uniqueStyle"));

        selenium.stop();
    }

}
